﻿
Partial Class pages_products_usercontrols_UCType
    Inherits System.Web.UI.UserControl

    Private m_type As CProductType
    Private m_parent As CProduct
    Private m_children As CProductList

    Public Sub Display(type As CProductType, parent As CProduct)
        m_type = type
        m_parent = parent
        m_children = parent.Children.GetByTypeId(type.TypeId)

        btnResetOrderToAlpha.Visible = m_children.Count > 1

        With type
            lnkType.Text = .Plural
            lnkType.NavigateUrl = CSitemap.ProductsForType(.TypeId)
            lnkType.ToolTip = "Show all products of this type"
            btnAdd.ToolTip = btnAdd.ToolTip.Replace("[Type]", .TypeName)
        End With

        plh.Controls.Clear() 'Only needed for ajax postbacks
        For Each i As CProduct In m_children
            UCSet(plh).Display(i, m_children)
        Next
    End Sub

#Region "Event Handlers"
    Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnAdd.Click
        Response.Redirect(CSitemap.ProductAdd(m_type.TypeId, m_parent.ProductId))
    End Sub
    Public Sub btnResetOrderToAlpha_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnResetOrderToAlpha.Click
        m_parent.Children.GetByTypeId(m_type.TypeId).SortAlphabetically()
    End Sub
#End Region

#Region "User Controls"
    Private Shared Function UCSet(ByVal target As Control) As pages_sets_usercontrols_UCSet
        Dim ctrl As Control = target.Page.LoadControl(CSitemap.UCSet)
        target.Controls.Add(ctrl)
        Return CType(ctrl, pages_sets_usercontrols_UCSet)
    End Function
#End Region

End Class
